/**
 * 解析ts类型
 * @param typeParam ts类型参数
 * @param {string} scriptContent script内容
 * @return {null|number|boolean|string|string}
 */
function relTSType(typeParam, scriptContent) {
    if (typeParam.type === 'TSLiteralType') {
        if (typeParam.literal.type === 'StringLiteral') {
            return "'" + typeParam.literal.value + "'";
        }
        // 文字类型
        return typeParam.literal.value
    } else if (typeParam.type === 'TSUnionType') {
        // 联合类型
        const types = []
        for (let type of typeParam.types) {
            const typeRes = relTSType(type, scriptContent)
            if (typeRes) {
                types.push(typeRes)
            }
        }
        return types.join("|")
    } else if (typeParam.type === 'TSArrayType') {
        // 数组类型
        return relTSType(typeParam.elementType, scriptContent) + '[]'
    } else if (typeParam.type === 'TSTypeReference') {
        // 引用类型
        return "~" + typeParam.typeName.name + "~"
    } else if (typeParam.type.startsWith('TS') && typeParam.type.endsWith('Keyword')) {
        // ts类型关键字
        // console.log(typeParam)
        const endIndex = typeParam.type.indexOf('Keyword')
        return typeParam.type.substring(2, endIndex).toLowerCase()
    } else if (typeParam.type === 'TSParenthesizedType') {
        // ts括号类型，如(1|2)[]
        return '(' + relTSType(typeParam.typeAnnotation, scriptContent) + ')'
    } else if (typeParam.type === 'TSFunctionType') {
        // ts 函数类型
        return scriptContent.substring(typeParam.start, typeParam.end)
    }
    return null;
}

exports.relTSType = relTSType;
